class AgentCreateSubordinateController{
    constructor($state,$scope,$rootScope,$q,AccountService,AgentService){
        Object.assign(this,{$state,$scope,$rootScope,$q,AccountService,AgentService});
        this.$rootScope.isAtLotteryPage = false;
        let currentState = $state.$current.name;
        this.usertype = "1";
        this.prList = [{key:0.5,name:"千分之五"},{key:0.4,name:"千分之四"},{key:0.3,name:"千分之三"},{key:0.2,name:"千分之二"},{key:0.1,name:"千分之一"},];
        this.profit_ratio=null;
        this.generated=false;
        let self = this;
        this.AccountService.getAccountInfo({ignoreLoadingBar:true}).then(_success).catch(_error);
        function _success(data) {
            if(data && data.profit_rate){
                let newList = new Array();
                self.prList.forEach(function (item, index, array) {
                    if (item.key <= data.profit_rate) {
                        newList.push(item);
                    }
                });
                self.prList = newList;
            }
        };
        function _error(reason) {
            //layer.msg(reason.text, {time: 3000});
            console.log(`AgentCreateSubordinateController.constructor.getAccountInfo(): ${reason}`);
        };
    }

    /**
     * 创建账户
     */
    create(){
        let self = this;
        let param = {};
        param.usertype = self.usertype;
        if(self.usertype==2){
            param.profit_ratio =0;
        }else{
            param.profit_ratio = self.profit_ratio.key;
        }
        param.username = self.username;
        self.AgentService.createSubordinate(param).then(_success).catch(_error);
        function _success(data) {
            let msg = "成功创建"+(self.usertype==2?"用户":"代理");
            if(data.message && data.message.code==200){
                layer.open({
                       icon:1,
                       title:"信息",
                       content:msg,
                       btn:["好的"],
                       yes:function (index,layerno) {
                           layer.close(index);
                           self.username="";
                           self.profit_ratio = "";
                           self.usertype = 1;
                       }
                   });
            }else{
                self.$q.reject(data.message);
            }
        }
        function _error(reason) {
            let type = typeof reason;
            layer.open({
                   icon:2,
                   title:"错误！",
                   content:type=='object'?reason.text:reason,
                   btn:["好的"],
                   yes:function (index,layerno) {
                       layer.close(index);
                   }
               });
        }
    }

    /**
     * 重置
     */
    reset(){
        this.usertype = "subordinate";
        this.username=null;
        this.profit_ratio=null;
    }

    /**
     * 生成推广链接
     */
    popularize(){
        let self = this;
        let param = {};
        param.profit_ratio=self.profit_ratio.key;
        param.usertype = self.usertype;
        self.AgentService.createPopulaUrl(param).then(_success).catch(_error);
        function _success(data) {
            self.popularize= data;
            self.generated=true;
        }
        function _error(reason) {
            console.log(`AgentCreateSubordinateController.popularize(): ${reason}`)
        }
    }
}
AgentCreateSubordinateController.$inject = ["$state","$scope","$rootScope","$q","AccountService","AgentService"];

export default AgentCreateSubordinateController;